/**
 * Created by liyun on 2015/10/27.
 */
var fs = require("fs");
var debug = require('debug')('routes:activity');
require('date-utils');
var activity = require('../models/activity').activity;

module.exports = function (app) {

    app.get('/activity/list', list);
    //app.get('/weixin', weixin);

    app.post('/activity/add', add);
    app.get('/activity/add', goAddPage);
    app.post('/activity/edit', edit);
    app.get('/activity/edit', goEditPage);
    app.post('/activity/uploadsThumb', uploadsThumb);


};


function uploadsThumb(req, res) {
    console.log('upload');
    var filepath = req.files.fileList.path;
    var name = req.files.fileList.name;
    fs.rename(filepath, 'public/images/activity/' + name, function (err) {
        if (err) {
            res.send(500);
        } else {
            //fs.unlinkSync(filepath);//ɾ���ļ�ԭ
            res.send('/images/activity/' + name);
        }

    });
};

function list(req, res) {
    var pagenum = 1;
    if (req.query.page) {
        pagenum = req.query.page;
    }
    getActivity(pagenum, req, res);

};

function goAddPage(req, res) {
    res.render('shop/activity_create');
};

function goEditPage(req, res) {
    var id = req.query.id;
    new activity().findOne(id, function (result) {
        res.render('shop/activity_edit', {activity: result[0]});
    });

};

function add(req, res) {
    var body = req.body;

    var filepath = req.files.thumb.path;
    body.thumb = '/images/activity/' + req.files.thumb.name;

    fs.rename(filepath, 'public' + body.thumb, function (err) {
        if (err) {
            res.send(500);
        } else {
            //fs.unlinkSync(filepath);//ɾ��ԭ�ļ�
            new activity().add(body, function (result) {
                getActivity(1, req, res);
            });
        }

    });


};

function edit(req, res) {
    var body = req.body;
    var filepath = req.files.thumb ? req.files.thumb.path : '';
    body.thumb = req.files.thumb ? '/images/activity/' + req.files.thumb.name : '';


    if (body.thumb != body.oldthumb) {
        var unlinkPath = 'public' + body.oldthumb;
        if (fs.exists(unlinkPath)) {
            fs.unlinkSync();
        }
    }

    if (filepath) {
        fs.rename(filepath, 'public' + body.thumb, function (err) {
            if (err) {
                res.send(500);
            } else {
                new activity().edit(body, function (result) {
                    getActivity(1, req, res);
                });
            }
        });
    } else {
        new activity().edit(body, function (result) {
            getActivity(1, req, res);
        });
    }
};

function getActivity(pagenum, req, res) {
    var page = ['activity.createtime', 'desc', (Number(pagenum) - 1) * 10, 10];//Ĭ��createtime����ÿҳ6��

    new activity().list(page, function (data) {
        debug(data);
        if (req.query.page) {
            res.send(data);
        } else {
            res.render('shop/activity_list', {data: data});
        }
        //
    });
}